﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nsBaseClass;
using log4net;
using System.Configuration;
using BMWSEMCRM.Model;
using BMWSEMCRM.NetUtils.Settings;
using BMWSEMCRM.Process;

namespace BMWSEMCRM
{
    class Program
    {
        private static readonly ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        //static void Main(string[] args)
        //{
        //    log4net.Config.XmlConfigurator.Configure();     //Configuring log
        //    //  parse xml file to List Oppoturnity
        //    List<Oppoturnity> list = new List<Oppoturnity>();
        //    Oppoturnity o = new Oppoturnity();
        //    Oppoturnity o2 = new Oppoturnity();
        //    list.Add(o);
        //    list.Add(o2);

        //    // Inssert to db
        //    DBAccess.InsertOppoturnity(list);

        //    logger.Debug("Run OK !");
        //    Console.ReadKey();
        //}


        public static void Main(string[] args)
        {
            try
            {
                log4net.Config.BasicConfigurator.Configure();
                if (args.Length == 1)
                {
                    var objGlobal = new clsGlobalVariable();                                    //clsGlovalVariable for connecting to SQL DB
                    objGlobal.InitFromCommandLine(ConfigurationSettings.AppSettings["DBName"],  //Get login data from app.config
                                                  ConfigurationSettings.AppSettings["WinAut"],
                                                  ConfigurationSettings.AppSettings["DBUser"],
                                                  ConfigurationSettings.AppSettings["DBPass"],
                                                  ConfigurationSettings.AppSettings["Site"]);

                    var taskName = args[0];
                    if ("import".Equals(taskName))
                    {
                        var delFile = "true".Equals(AppConfig.GetSettingValue("DeleteImportedFile"));
                        var impFolder = AppConfig.GetSettingValue("ImportFolder");
                        var backupLocation = AppConfig.GetSettingValue("BackupLocation");
                        var task = new ImportFile();

                        task.DelFile = delFile;
                        task.ImportFolder = impFolder;
                        task.BackupLocation = backupLocation;

                        // start
                        logger.Info("Start import task");

                        task.Start();
                        // end
                        logger.Info("Complete import task");
                    }
                    else if ("export".Equals(taskName))
                    {
                        logger.Info("Start export task");
                    }
                }
                else
                {
                    var msg = "Usage: BMWSEMCRM.exe [import | export]";
                    logger.Info(msg);
                    Console.WriteLine(msg);
                }
            }
            catch (Exception ex)
            {
                logger.Fatal(ex.Message, ex);
            }
        }
    }
}
